package com.wujie.mall.domain.order;

import lombok.Data;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

/**
 * 收货地址实体
 * 
 * @author wujie
 * @since 2024-08-26
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ShippingAddress {

    /**
     * 地址ID
     */
    private Long id;

    /**
     * 订单ID
     */
    private Long orderId;

    /**
     * 收货人姓名
     */
    private String receiver;

    /**
     * 联系电话
     */
    private String phone;

    /**
     * 省份
     */
    private String province;

    /**
     * 城市
     */
    private String city;

    /**
     * 区县
     */
    private String district;

    /**
     * 详细地址
     */
    private String detail;

    /**
     * 邮政编码
     */
    private String postalCode;

    /**
     * 获取完整地址
     * @return 完整地址字符串
     */
    public String getFullAddress() {
        StringBuilder fullAddress = new StringBuilder();
        
        if (province != null && !province.trim().isEmpty()) {
            fullAddress.append(province);
        }
        
        if (city != null && !city.trim().isEmpty()) {
            if (fullAddress.length() > 0) {
                fullAddress.append(" ");
            }
            fullAddress.append(city);
        }
        
        if (district != null && !district.trim().isEmpty()) {
            if (fullAddress.length() > 0) {
                fullAddress.append(" ");
            }
            fullAddress.append(district);
        }
        
        if (detail != null && !detail.trim().isEmpty()) {
            if (fullAddress.length() > 0) {
                fullAddress.append(" ");
            }
            fullAddress.append(detail);
        }
        
        return fullAddress.toString();
    }

    /**
     * 获取格式化的收货信息
     * @return 格式化的收货信息
     */
    public String getFormattedInfo() {
        return String.format("%s，%s，%s", receiver, phone, getFullAddress());
    }

    /**
     * 验证收货地址
     * @throws IllegalStateException 地址信息不完整时抛出
     */
    public void validate() {
        if (receiver == null || receiver.trim().isEmpty()) {
            throw new IllegalStateException("收货人姓名不能为空");
        }
        
        if (phone == null || phone.trim().isEmpty()) {
            throw new IllegalStateException("联系电话不能为空");
        }
        
        // 验证手机号格式（简单验证）
        if (!phone.matches("^1[3-9]\\d{9}$")) {
            throw new IllegalStateException("联系电话格式不正确");
        }
        
        if (province == null || province.trim().isEmpty()) {
            throw new IllegalStateException("省份不能为空");
        }
        
        if (city == null || city.trim().isEmpty()) {
            throw new IllegalStateException("城市不能为空");
        }
        
        if (district == null || district.trim().isEmpty()) {
            throw new IllegalStateException("区县不能为空");
        }
        
        if (detail == null || detail.trim().isEmpty()) {
            throw new IllegalStateException("详细地址不能为空");
        }
        
        // 验证地址长度
        if (detail.length() > 200) {
            throw new IllegalStateException("详细地址长度不能超过200个字符");
        }
    }

    /**
     * 检查是否为同一地址
     * @param other 另一个地址
     * @return true-相同地址，false-不同地址
     */
    public boolean isSameAddress(ShippingAddress other) {
        if (other == null) {
            return false;
        }
        
        return receiver.equals(other.receiver) &&
               phone.equals(other.phone) &&
               province.equals(other.province) &&
               city.equals(other.city) &&
               district.equals(other.district) &&
               detail.equals(other.detail);
    }
}